<html>
<head>
<script src="../../resources/js-test.js"></script>
<style>
/* Test 1: Invalid font data */
@font-face {
    font-family: myfont1;
    src: url('resources/invalidfont.png') format("opentype");
}

/* Test 2: Download error */
@font-face {
    font-family: myfont2;
    src: url('resources/DownLoadErrorAhem.otf');
}

/* Test 3: Empty data url */
@font-face {
    font-family: myfont3;
    src: url(data:application/x-truetype-font,) format("truetype");
}

/* Test 4: Download error followed by existing local font */
@font-face {
    font-family: myfont4;
    src: url('resources/DownLoadErrorAhem.otf'), local('Courier New'), local(Cousine Regular);
}

/* Test 5: Multiple errors */
@font-face {
    font-family: myfont5;
    src: url('resources/DownLoadErrorAhem.otf'), url(data:application/x-truetype-font,) format("truetype");
}
</style>
<script>
description('Test download error cases.');

window.jsTestIsAsync = true;

function runTests() {
    document.fonts.addEventListener('loading', onLoading);
    document.fonts.addEventListener('loadingdone', onLoadingDone);
    document.fonts.addEventListener('loadingerror', onLoadingError);

    document.fonts.load('10px myfont1').catch(function() {
        document.fonts.load('10px myfont2, myfont3, myfont4, myfont5').catch(function(){});
      });
    document.fonts.ready.then(finish);
}

var event;
var firedEvents = [];

function onLoading(e) {
    firedEvents.push(e.type);
}

function onLoadingDone(e) {
    firedEvents.push(e.type);
    event = e;
    shouldBe('event.fontfaces.length', '1');
    shouldBeEqualToString('event.fontfaces[0].family', 'myfont4');
}

function onLoadingError(e) {
    firedEvents.push(e.type);
    event = e;
    shouldBe('event.fontfaces.length', '4');
    failedFonts = e.fontfaces.map(function(face){return face.family;}).sort();
    shouldBe('failedFonts', "['myfont1', 'myfont2', 'myfont3', 'myfont5']");
}

function finish() {
    shouldBe("firedEvents", "['loading', 'loadingdone', 'loadingerror']");
    finishJSTest();
}

if (document.fonts)
    runTests();
else {
    testFailed('document.fonts does not exist');
    finishJSTest();
}
</script>
</head>
</html>
